package cn.darkhorse.common.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.util.PoiMergeCellUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author yuanjun
 * @since 2021/8/11 15:01
 */
public class ReportExportUtil {

    public static String exportExcel(String fileName, String templatePath, Map<String, Object> value) throws IOException {
        TemplateExportParams params = new TemplateExportParams(templatePath);
        params.setColForEach(true);
        Workbook book = ExcelExportUtil.exportExcel(params, value);
        //合计格式化
        changeTypeToNumber(book);
        String realFileName = encodingFilename(fileName);
        FileOutputStream fos = new FileOutputStream(getAbsoluteFile(realFileName));

        book.write(fos);
        fos.close();
        return realFileName;
    }

    public static String exportExcel(String fileName, String templatePath, Map<String, Object> value,MergeParams mergeParams) throws IOException {
        TemplateExportParams params = new TemplateExportParams(templatePath);
        params.setColForEach(true);
        Workbook book = ExcelExportUtil.exportExcel(params, value);

        PoiMergeCellUtil.mergeCells(book.getSheetAt(mergeParams.getSheetNum()),mergeParams.getStartRow()
                ,mergeParams.getColumns().toArray(new Integer[mergeParams.getColumns().size()]));
        //合计格式化
        changeTypeToNumber(book);
        String realFileName = encodingFilename(fileName);
        FileOutputStream fos = new FileOutputStream(getAbsoluteFile(realFileName));

        book.write(fos);
        fos.close();
        return realFileName;
    }

    public static String exportExcel(String fileName, Class clazz, List list) throws IOException {
        ExportParams params = new ExportParams(fileName,"sheet1");
        Workbook book = ExcelExportUtil.exportExcel(params,clazz, list);

        String realFileName = encodingFilename(fileName);
        FileOutputStream fos = new FileOutputStream(getAbsoluteFile(realFileName));

        book.write(fos);
        fos.close();
        return realFileName;
    }


    /**
     * 获取下载路径
     *
     * @param filename 文件名称
     */
    public static String getAbsoluteFile(String filename)
    {
        String downloadPath = RuoYiConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }

    public static String encodingFilename(String filename)
    {
        filename = UUID.randomUUID().toString() + "_" + filename + ".xlsx";
        return filename;
    }

    /**
     * 合计行数字格式化
     * @param book
     * @author mao
     * @since 20220927
     */
    public static void changeTypeToNumber(Workbook book){
        CellStyle numberCellStyle = book.createCellStyle();
        numberCellStyle.setBorderBottom(BorderStyle.THIN);
        numberCellStyle.setBorderLeft(BorderStyle.THIN);
        numberCellStyle.setBorderTop(BorderStyle.THIN);
        numberCellStyle.setBorderRight(BorderStyle.THIN);
        numberCellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.0000"));
        numberCellStyle.setWrapText(true);
        Sheet sheet = book.getSheetAt(0);
        Row row = sheet.getRow(sheet.getLastRowNum());
        if(null!=row.getCell(0)&&row.getCell(0).getCellType()==CellType.STRING){
            if("合计".equals(row.getCell(0).getStringCellValue())||"合计：".equals(row.getCell(0).getStringCellValue())){
                for(int i = 1; i < row.getLastCellNum(); i++) {
                    Cell cell = row.getCell(i);
                    if (cell != null&&cell.getCellType()==CellType.STRING) {
                        if (StringUtils.isNotEmpty(cell.getStringCellValue())) {
                            cell.setCellValue(Double.parseDouble(cell.getStringCellValue()));
                            cell.setCellStyle(numberCellStyle);
                        }
                    }
                }
            }
        }
    }
}
